const { Router } = require("express");
const { SuccessResult, ErrorResult } = require("../utils");
const YJwt = require("../tools/jwt");
const { timeout } = require("../../config/token.config");

const YRouter = Router();

YRouter.get("/getUserInfo", (req, res) => {
  // if(!req.session.isLogin) {
  //   return res.send(ErrorResult("请先登录！"))
  // }

  res.send(SuccessResult("请求成功", req.user));
});

YRouter.post("/logout", (req, res) => {
  req.session.destroy();
  res.send(SuccessResult("操作成功！"));
});

/**
 * 登录接口
 */
YRouter.post("/login", (req, res) => {
  const user = req.body;
  if(user.username !== 'admin' || user.password !== '111111') {
    return res.send(ErrorResult("登录失败！"))
  }
  // req.session.user = req.body;
  // req.session.isLogin = true;
  const resultData = {
    token: YJwt.encodeToken(user.username),
    timeout: timeout
  }
  res.send(SuccessResult("登录成功！", resultData));
})

module.exports = YRouter;